const bcrypt = require('bcryptjs')
const { LinValidator, Rule } = require('../../core/lin-validator-v2')
const {User} =require('../models/user')
const {LoginType} = require('../lib/enum')
class PositiveIntegerValidator extends LinValidator {
  constructor() {
    super()
    this.id = [new Rule('isInt', '需要是正整数', { min: 1 })]
  }
}
// 注册用户信息校验
class RegisterValidator extends LinValidator {
  constructor() {
    super()
    this.email = [new Rule('isEmail', '不符合Email规范')]
    this.password1 = [
      new Rule('isLength', '密码至少6个字符，最多32个字符', {
        min: 6,
        max: 32,
      }),
      new Rule('matches', '密码不符合规范','^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]'),
    ]
    this.password2 = this.password1
    this.nickname= [
      new Rule('isLength', '不符合长度规范', {
        min: 4,
        max: 32,
      })
    ]
  }
  validatePassword(vals){
    const psw1 = vals.body.password1
    const psw2 = vals.body.password2
    if (psw1 !== psw2) {
      throw new Error('两个密码必须相同')
    }
  }
  async validateEmail(vals){
    const email = vals.body.email
    const user =await User.findOne({
      where:{
        email
      }
    })
    if (user) {
      throw new Error('emali已存在')
    }
  }
}
// 修改用户信息校验
class UpdateValidator extends LinValidator {
  constructor(){
    super()
    this.email = [new Rule('isEmail', '不符合Email规范')]
    this.password = [
      new Rule('isLength', '密码至少6个字符，最多32个字符', {
        min: 6,
        max: 32,
      }),
      new Rule('matches', '密码不符合规范','^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]'),
    ]
    this.oldPassword = this.password
    this.nickname= [
      new Rule('isLength', '不符合长度规范', {
        min: 4,
        max: 32,
      })
    ]
  }
  async validatePassword(vals){
    const oldPsw = vals.body.oldPassword
    const email = vals.body.email
    const result = await User.findOne({
      where:{
        email
      }
    })
    if (result) {
      let psw = result.dataValues.password
      const correct = bcrypt.compareSync(oldPsw, psw)
      if (!correct) {
        throw new Error('请输入正确的原始密码')
      }
    }
   
  }
}

// Token校验
class TokenValidator extends LinValidator {
  constructor() {
    super()
    this.account = [new Rule('isLength', '不符合账号规则',{
      min:4,
      max:32
    })]
    this.secret = [
      new Rule('isOptional'),
      new Rule('isLength', '至少6个字符',{
        min:6,
        max:128
      })
    ]
  }
    //  checkType(vals)
  validateLoginType(vals) {
    if (!vals.body.type) {
        throw new Error('type是必须参数')
    }
    if (!LoginType.isThisType(vals.body.type)) {
      throw new Error('type参数不合法')
    }
  }
}
// 空值校验
class NotEmptyValidator extends LinValidator {
  constructor() {
    super()
    this.token = [new Rule('isLength', '不允许为空',{
      min:1,
    })]
  }
}
// 用户删除id验证
class DeleteValidator extends LinValidator {
  constructor() {
    super()
    this.id = [new Rule('isEmail', '不允许为空')]
  }
}
// 分页参数验证
class PageValidator extends LinValidator {
  constructor() {
    super()
    this.querynum = [new Rule('isLength', '不允许为空',{min:1})]
    this.querysize = [new Rule('isLength', '不允许为空',{min:1})]
  }
}

function checkType(vals){
  if (!vals.body.type) {
    throw new Error('type是必须参数')
  }
  if (!LoginType.isThisType(vals.body.type)) {
    throw new Error('type参数不合法')
    
  }
}

class LikeValidator extends PositiveIntegerValidator {
  constructor() {
    super()
    this.validateType = checkType
  }
}
module.exports = {
  PositiveIntegerValidator,
  RegisterValidator,
  TokenValidator,
  NotEmptyValidator,
  UpdateValidator,
  DeleteValidator,
  PageValidator,
  LikeValidator
}
